package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.User;
import com.campsys.service.AdminStudentService;
import com.campsys.service.dto.StudentDTO;
import com.campsys.service.dto.StudentQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/admin/student")
@Slf4j
public class AdminStudentController {
    
    @Autowired
    private AdminStudentService studentService;
    
    @RequiresRoles("admin")
    @PostMapping("/list")
    public Result getStudentList(@RequestBody StudentQueryDTO queryDTO) {
        Page<User> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return Result.ok(studentService.getStudentList(page, queryDTO));
    }
    
    @RequiresRoles("admin")
    @PostMapping("/add")
    public Result addStudent(@RequestBody @Validated StudentDTO studentDTO) {
        studentService.addStudent(studentDTO);
        return Result.ok();
    }
    
    @RequiresRoles("admin")
    @PostMapping("/update")
    public Result updateStudent(@RequestBody @Validated StudentDTO studentDTO) {
        if (studentDTO.getId() == null) {
            return Result.error("学生ID不能为空");
        }
        studentService.updateStudent(studentDTO);
        return Result.ok();
    }
    
    @RequiresRoles("admin")
    @PostMapping("/delete/{id}")
    public Result deleteStudent(@PathVariable Long id) {
        studentService.deleteStudent(id);
        return Result.ok();
    }
    
    @RequiresRoles("admin")
    @PostMapping("/status/{id}")
    public Result updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        studentService.updateStatus(id, status);
        return Result.ok();
    }
    
    @RequiresRoles("admin")
    @PostMapping("/import")
    public Result importStudents(@RequestParam("file") MultipartFile file) {
        List<StudentDTO> students = studentService.importStudents(file);
        return Result.ok(students);
    }
    
    @RequiresRoles("admin")
    @PostMapping("/audit")
    public Result auditStudents(@RequestBody List<Long> studentIds) {
        studentService.auditStudents(studentIds);
        return Result.ok();
    }
} 